﻿using DevExpress.XtraEditors;
using ImageDefine;
using SmartDisplayControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableModule;

namespace CustomModule
{
    public partial class FormOperator : DevExpress.XtraEditors.XtraForm
    {
        public FormOperator()
        {
            InitializeComponent();
        }

        private void FormOperator_Load(object sender, EventArgs e)
        {
            TableModule.FormDebugSimple formDebugSimple = new TableModule.FormDebugSimple();
            formDebugSimple.TopLevel = false;
            formDebugSimple.Size = panelControl1.Size;
            panelControl1.Controls.Add(formDebugSimple);
            formDebugSimple.Show();
            if (FrameManager.MachineStatus.Is4Axis == false)
            {
                //simpleButton计算工具.Visible = false;
                simpleButton验证工具.Visible = false;
            }
            TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");
            textEdit针头基准位置.Text= tablePos.dZPos.ToString();
            timer1.Enabled = true;
        }
        private void simpleButton计算工具_Click(object sender, EventArgs e)
        {
            if (FrameManager.MachineStatus.Is4Axis)
            {
                TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");

                double dCurrentX = tablePos.dXPos;
                double dCurrentY = tablePos.dYPos;
                TableManager.tableRunner.UpdateTool("Nozzle", dCurrentX, dCurrentY);
                TableManager.Save();
            }
            else
            {
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dZPos = tableDataPosition.dZPos;
                TableManager.Save();
            }

        }

        private void simpleButton移动到对针头上方_Click(object sender, EventArgs e)
        {
            if (FrameManager.MachineStatus.Is4Axis)
            {
                TableManager.tableRunner.SetTool("Nozzle");
                TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");
                tablePos.dZPos = 0;
                TableManager.tableRunner.Jump(tablePos);
                TableManager.tableRunner.SetTool("Default");
            }
            else
            {
                TableManager.tableRunner.SetTool("Default");
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX = 0.0;
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY = 0.0;
                TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");
                tablePos.dZPos = 0;
                TableManager.tableRunner.Jump(tablePos);
                TableManager.Save();
            }
        }

        private void simpleButton验证工具_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                ToolConfirm();
            });
        }
        public void ToolConfirm()
        {
            TableManager.tableRunner.SetTool("Nozzle");
            TableManager.tableRunner.Jump("针头工具位置");
            System.Threading.Thread.Sleep(2000);
            TableDataPosition position = TableManager.tableRunner.GetPosition("针头工具位置");
            position.dUPos = position.dUPos - 30;
            TableManager.tableRunner.Jump(position);
            //System.Threading.Thread.Sleep(1000);
            TableManager.tableRunner.SetTool("Default");


            XtraMessageBox.Show("确定针头是否和基准对准");
            TableManager.tableRunner.Jump("SafePos");
        }

        private void simpleButton移动到安全位置_Click(object sender, EventArgs e)
        {
            TableManager.tableRunner.SetTool("Default");
            TableDataPosition position = TableManager.tableRunner.GetPosition("SafePos");
            position.dUPos = position.dUPos - 30;
            TableManager.tableRunner.Jump(position, false);
        }

        private void simpleButton移动到对针头位置_Click(object sender, EventArgs e)
        {
            if (FrameManager.MachineStatus.Is4Axis)
            {
                TableManager.tableRunner.SetTool("Nozzle");
                TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");
                TableManager.tableRunner.Jump(tablePos);
                TableManager.tableRunner.SetTool("Default");
            }
            else
            {
                TableManager.tableRunner.SetTool("Default");
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX = 0.0;
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY = 0.0;
                TableDataPosition tablePos = TableManager.tableRunner.GetPosition("针头工具位置");
                TableManager.tableRunner.Jump(tablePos);
                TableManager.Save();
            }
        }

        private void simpleButton禁止出胶_Click(object sender, EventArgs e)
        {
            FrameManager.FrameManager.ProductSetting.禁止出胶 = true;
        }

        private void simpleButton正常出胶_Click(object sender, EventArgs e)
        {
            FrameManager.FrameManager.ProductSetting.禁止出胶= false;
        }

        private void simpleButton禁止排胶_Click(object sender, EventArgs e)
        {
            FrameManager.FrameManager.ProductSetting.禁止自动排胶 = true;
        }

        private void simpleButton正常排胶_Click(object sender, EventArgs e)
        {
            FrameManager.FrameManager.ProductSetting.禁止自动排胶 = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            if (FrameManager.FrameManager.ProductSetting.禁止出胶)
            {
                simpleButton禁止出胶.Appearance.BackColor = Color.Green;
                simpleButton正常出胶.Appearance.BackColor = Color.FromKnownColor(KnownColor.Control);
            }
            else
            {
                simpleButton禁止出胶.Appearance.BackColor = Color.FromKnownColor(KnownColor.Control);
                simpleButton正常出胶.Appearance.BackColor = Color.Green;
            }
            if (FrameManager.FrameManager.ProductSetting.禁止自动排胶)
            {
                simpleButton禁止排胶.Appearance.BackColor = Color.Green;
                simpleButton正常排胶.Appearance.BackColor = Color.FromKnownColor(KnownColor.Control);
            }
            else
            {
                simpleButton禁止排胶.Appearance.BackColor = Color.FromKnownColor(KnownColor.Control);
                simpleButton正常排胶.Appearance.BackColor = Color.Green;
            }
        }
    }
}